<?php

namespace app\common\model;

use think\Db;
use think\Model;

class ShopMoneyLog extends Model{
    protected $pk = 'id';

//    protected $table = 'shop_money_log';

    public static function getAdd($order){
        $pu_deduct = get_deduct($order['sell_id'],'pu_deduct');//普通商品扣点
        $bao_deduct = get_deduct($order['sell_id'],'bao_deduct');//爆品扣点
        $data['shop_id'] = $order['sell_id'];
        $data['money']   = $order['pay_price'];//订单金额

        $admin = Admin::get($data['shop_id']);

        $data['desc']     = $order['order_sn'].'订单结算';
        $data['status']   = 1;
        $data['order_sn'] = $order['order_sn'];
        $data['createtime'] = time();
//        dd($order['id']);
        $ordergoods = OrderGoods::where(['order_id'=>$order['id']])->select();
        $bao_price=0;
        foreach ($ordergoods as $k=>$v){
            if($v['goods_type']==3){
                $bao_price += $v['goods_price']*$v['goods_num'];
            }
        }


        $money = (int) ($data['money']*100);
        $merchant = (int) ($money * $pu_deduct /100);
        $bao_merchant = (int) ($bao_price * $bao_deduct);

        $data['merchant']   =  $merchant /100;//扣款

        $data['bao_merchant'] = $bao_merchant/100;//爆品扣点

        $data['price']   = ($money - $merchant -  $bao_merchant )/100 ;//实际金额

        $data['before']   = $admin['money'];
        $data['after']    = $admin['money']+$data['price'] ;

//        if($data['order_sn']=='2020120948985098'){
//            dd($data);
//        }
//        dump($data);
        if(self::create($data)){
            $admin->money   = $data['after'];
            $admin->save();
        }
    }

}